package ru.ifmo.mlarinsky.classificator.model;

import java.util.*;
import java.util.Map.Entry;

/**
 * @author Mikhail Larinskiy
 */
public class Vector {
    private final String label;
    private final String[] dimensions;
    private final Double[] values;

    public Vector(String label, String[] dimensions, Double[] values) {
        this.label = label;
        this.dimensions = dimensions;
        this.values = values;
    }

    public String getLabel() {
        return label;
    }

    public String[] getDimensions() {
        return dimensions;
    }

    public Double[] getValues() {
        return values;
    }

    public List<Map.Entry<String, Double>> getSortedCoordinates() {
        List<Map.Entry<String, Double>> sortedCoordinates = new ArrayList<Map.Entry<String, Double>>();

        for (int i = 0, length = values.length; i < length; i++) {
            sortedCoordinates.add(new AbstractMap.SimpleEntry<String, Double>(dimensions[i], values[i]));
        }

        Collections.sort(sortedCoordinates, new Comparator<Map.Entry<String, Double>>() {
            public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
                if (o1 == null)
                    return o2 != null ? 1 : 0;
                if (o2 == null)
                    return 1;

                return (-1) * o1.getValue().compareTo(o2.getValue());
            }
        });

        return Collections.unmodifiableList(sortedCoordinates);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(label);
        sb.append(":");
        List<Entry<String, Double>> sortedIndexes = getSortedCoordinates();
        for (Entry<String, Double> entry : sortedIndexes)
            sb.append("\n\t").append(entry.getKey()).append(" -> ").append(entry.getValue());

        return sb.toString();
    }
}
